#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdatomic.h>
#include "somb.h"

#include "BasicCom.pb.h"

//0:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1 
//1:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2
uint32_t request_type=0;//default=BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1

void consumer_setup_test_param(int *count,int* threads)
{
    printf("input how many times you want to request\n");
    scanf("%d",count);

    printf("input how many thread you want to send request at same time\n");
    scanf("%d",threads);
}

static uint32_t loop_setrequest = 0;
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
void *consumer_send_method_setrequest_thread(void *arg)
{
    pthread_t threadid = pthread_self();
    int count = *(int *)arg; 

    printf("count=%d,threadid=%d\n",count,(int)threadid);

    int loop=0;
    for(loop=0;loop<count;loop++)
    {

        st_basic_com_form_setreq msg_setreq = st_basic_com_form_setreq_init_zero;
        msg_setreq.sng_int32 = 1;
        msg_setreq.sng_int64 = 2;
        msg_setreq.sng_uint32 = 3; 
        msg_setreq.sng_uint64 = 4;  
        msg_setreq.sng_sint32 = -5;  
        msg_setreq.sng_sint64 = -6; 
        msg_setreq.sng_bool = true;    
        msg_setreq.sng_fixed32 = 8; 
        msg_setreq.sng_sfixed32 = 9;
        msg_setreq.sng_float = 0.10;  
                                
        msg_setreq.sng_fixed64 = 11; 
        msg_setreq.sng_sfixed64 = 12;
        msg_setreq.sng_double = 0.13;

        uint8_t sng_string_send_buf[16]="14";
        SOMB_INIT_BYTES_ENCODE_BUFFER(msg_setreq.sng_string,sng_string_send_buf,sizeof(sng_string_send_buf));

        sprintf(msg_setreq.sng_max_string,"15");
        sprintf(msg_setreq.sng_fix_string,"16");


        uint8_t sng_bytes_send_buf[16]="17";
        SOMB_INIT_BYTES_ENCODE_BUFFER(msg_setreq.sng_bytes,sng_bytes_send_buf,sizeof(sng_bytes_send_buf));

        msg_setreq.sng_max_bytes.size=2;
        sprintf(msg_setreq.sng_max_bytes.bytes,"18");

        sprintf(msg_setreq.sng_fix_bytes,"19");

        msg_setreq.has_sng_submsg = true;
        msg_setreq.sng_submsg.sub_int32 = 20;

        msg_setreq.sng_enum = MyEnum_Truth;

        msg_setreq.has_sng_emptymsg = true;
        msg_setreq.sng_emptymsg.dummy_field = 'x';


        msg_setreq.rep_int32_count=5;
        msg_setreq.rep_int32[0]=31;
        msg_setreq.rep_int32[1]=31;
        msg_setreq.rep_int32[2]=31;
        msg_setreq.rep_int32[3]=31;
        msg_setreq.rep_int32[4]=31;


        msg_setreq.rep_int64_count=5;
        msg_setreq.rep_int64[0]=32;
        msg_setreq.rep_int64[1]=32;
        msg_setreq.rep_int64[2]=32;
        msg_setreq.rep_int64[3]=32;
        msg_setreq.rep_int64[4]=32;

        msg_setreq.rep_uint32_count=5;
        msg_setreq.rep_uint32[0]=33;
        msg_setreq.rep_uint32[1]=33;
        msg_setreq.rep_uint32[2]=33;
        msg_setreq.rep_uint32[3]=33;
        msg_setreq.rep_uint32[4]=33;

        msg_setreq.rep_uint64_count=5;
        msg_setreq.rep_uint64[0]=34;
        msg_setreq.rep_uint64[1]=34;
        msg_setreq.rep_uint64[2]=34;
        msg_setreq.rep_uint64[3]=34;
        msg_setreq.rep_uint64[4]=34;


        msg_setreq.rep_sint32_count=5;
        msg_setreq.rep_sint32[0]=-35;
        msg_setreq.rep_sint32[1]=-35;
        msg_setreq.rep_sint32[2]=-35;
        msg_setreq.rep_sint32[3]=-35;
        msg_setreq.rep_sint32[4]=-35;

        msg_setreq.rep_sint64_count=5;
        msg_setreq.rep_sint64[0]=-36;
        msg_setreq.rep_sint64[1]=-36;
        msg_setreq.rep_sint64[2]=-36;
        msg_setreq.rep_sint64[3]=-36;
        msg_setreq.rep_sint64[4]=-36;

        msg_setreq.rep_bool_count=5;
        msg_setreq.rep_bool[0]=true;
        msg_setreq.rep_bool[1]=true;
        msg_setreq.rep_bool[2]=true;
        msg_setreq.rep_bool[3]=true;
        msg_setreq.rep_bool[4]=true;

        msg_setreq.rep_fixed32_count=5;
        msg_setreq.rep_fixed32[0]=38;
        msg_setreq.rep_fixed32[1]=38;
        msg_setreq.rep_fixed32[2]=38;
        msg_setreq.rep_fixed32[3]=38;
        msg_setreq.rep_fixed32[4]=38;

        msg_setreq.rep_sfixed32_count=5;
        msg_setreq.rep_sfixed32[0]=39;
        msg_setreq.rep_sfixed32[1]=39;
        msg_setreq.rep_sfixed32[2]=39;
        msg_setreq.rep_sfixed32[3]=39;
        msg_setreq.rep_sfixed32[4]=39;


        msg_setreq.rep_float_count=5;
        msg_setreq.rep_float[0]=0.40;
        msg_setreq.rep_float[1]=0.40;
        msg_setreq.rep_float[2]=0.40;
        msg_setreq.rep_float[3]=0.40;
        msg_setreq.rep_float[4]=0.40;

        msg_setreq.rep_fixed64_count=5;
        msg_setreq.rep_fixed64[0]=41;
        msg_setreq.rep_fixed64[1]=41;
        msg_setreq.rep_fixed64[2]=41;
        msg_setreq.rep_fixed64[3]=41;
        msg_setreq.rep_fixed64[4]=41;

        msg_setreq.rep_sfixed64_count=5;
        msg_setreq.rep_sfixed64[0]=42;
        msg_setreq.rep_sfixed64[1]=42;
        msg_setreq.rep_sfixed64[2]=42;
        msg_setreq.rep_sfixed64[3]=42;
        msg_setreq.rep_sfixed64[4]=42;
        

        msg_setreq.rep_double_count=5;
        msg_setreq.rep_double[0]=0.43;
        msg_setreq.rep_double[1]=0.43;
        msg_setreq.rep_double[2]=0.43;
        msg_setreq.rep_double[3]=0.43;
        msg_setreq.rep_double[4]=0.43;

        uint8_t rep_string_send_buf[16]="44";
        SOMB_INIT_BYTES_ENCODE_BUFFER(msg_setreq.rep_string,rep_string_send_buf,sizeof(rep_string_send_buf));
        
        msg_setreq.rep_max_string_count=5;
        sprintf(msg_setreq.rep_max_string[0],"45");
        sprintf(msg_setreq.rep_max_string[1],"45");
        sprintf(msg_setreq.rep_max_string[2],"45");
        sprintf(msg_setreq.rep_max_string[3],"45");
        sprintf(msg_setreq.rep_max_string[4],"45");

        msg_setreq.rep_fix_string_count=5;
        sprintf(msg_setreq.rep_fix_string[0],"46");
        sprintf(msg_setreq.rep_fix_string[1],"46");
        sprintf(msg_setreq.rep_fix_string[2],"46");
        sprintf(msg_setreq.rep_fix_string[3],"46");
        sprintf(msg_setreq.rep_fix_string[4],"46");



        uint8_t rep_bytes_send_buf[16]="47";
        SOMB_INIT_BYTES_ENCODE_BUFFER(msg_setreq.rep_bytes,rep_bytes_send_buf,sizeof(rep_bytes_send_buf));

        msg_setreq.rep_max_bytes_count=5;
        msg_setreq.rep_max_bytes[0].size=2;
        sprintf(msg_setreq.rep_max_bytes[0].bytes,"48");
        msg_setreq.rep_max_bytes[1].size=2;
        sprintf(msg_setreq.rep_max_bytes[1].bytes,"48");
        msg_setreq.rep_max_bytes[2].size=2;
        sprintf(msg_setreq.rep_max_bytes[2].bytes,"48");
        msg_setreq.rep_max_bytes[3].size=2;
        sprintf(msg_setreq.rep_max_bytes[3].bytes,"48");
        msg_setreq.rep_max_bytes[4].size=2;
        sprintf(msg_setreq.rep_max_bytes[4].bytes,"48");

        msg_setreq.rep_fix_bytes_count=5;
        sprintf(msg_setreq.rep_fix_bytes[0],"49");
        sprintf(msg_setreq.rep_fix_bytes[1],"49");
        sprintf(msg_setreq.rep_fix_bytes[2],"49");
        sprintf(msg_setreq.rep_fix_bytes[3],"49");
        sprintf(msg_setreq.rep_fix_bytes[4],"49");


        msg_setreq.rep_submsg_count=5;
        msg_setreq.rep_submsg[0].sub_int32=50;
        msg_setreq.rep_submsg[1].sub_int32=50;
        msg_setreq.rep_submsg[2].sub_int32=50;
        msg_setreq.rep_submsg[3].sub_int32=50;
        msg_setreq.rep_submsg[4].sub_int32=50;

        msg_setreq.rep_enum_count=5;
        msg_setreq.rep_enum[0]=MyEnum_Truth;
        msg_setreq.rep_enum[1]=MyEnum_Truth;
        msg_setreq.rep_enum[2]=MyEnum_Truth;
        msg_setreq.rep_enum[3]=MyEnum_Truth;
        msg_setreq.rep_enum[4]=MyEnum_Truth;
        
        msg_setreq.which_oneof_data=st_basic_com_form_setreq_oneof_msg1_tag;
        msg_setreq.oneof_data.oneof_msg1.sub_int32 = 60;

        int32_t ret;

        //如果proto定义的是定长结构体，则直接使用xxx_size，用于静态定义定义buffer
        //如果proto定义的是非定长，含有动态长度成员，比如string/bytes，proto不会生成xxx_size,这个时候需要使用动态定义的buffer
        //这个例子中，st_basic_com_form_setreq结构体在proto中定义存在以下4个动态的成员，
        //sng_string,sng_bytes,rep_string,rep_bytes, 所以需要动态申请最大限度的buffer作为ENCODE后数据的载体
        uint32_t buffersize=sizeof(st_basic_com_form_setreq)+ /*基本结构体大小*/
                    sizeof(sng_string_send_buf)+/*动态buf大小*/
                    sizeof(sng_bytes_send_buf)+/*动态buf大小*/
                    sizeof(rep_string_send_buf)+/*动态buf大小*/
                    sizeof(rep_bytes_send_buf)/*动态buf大小*/
                    ;
        //动态的malloc申请内存，buffersize是最极端、最大可能会被承载的encode数据载体大小
        uint8_t *pbuffer = SOMB_ALLOC(buffersize);

        //将msg_setreq进行encode,encode后的数据将存放到pbuffer中，encode后的数据也将放到buffersize中
        //注意：buffersize是INOUT参数(即输入也输出),输入的是pbuffer的实际内存占用大小，输出的是encode数据实际占用的大小
        int32_t error;
        error = SOMB_MESSAGE_ENCODE(st_basic_com_form_setreq,&msg_setreq,pbuffer,&buffersize);
        if( 0 != error )
        {
            printf("consumer method setrequest encode failed\n");
            SOMB_FREE(pbuffer);
            pthread_exit(&threadid);
        }

        ret = i32SOMB_Method_SetRequest(BASICCOM_SID_METHOD_SETREQUST_TEST,pbuffer,buffersize);
        if(SOMB_OK != ret)
        {
            printf("consumer method set request failed,loop=%d\n",loop_setrequest);
            SOMB_FREE(pbuffer);
            pthread_exit(&threadid);
        }
        else
        {
            printf("consumer method set request success,loop=%d\n",loop_setrequest);
        }

        //释放动态申请的buffer
        SOMB_FREE(pbuffer);
    }

    printf("consumer setrequest thread(%d) exit!\n",(int)threadid);
    return NULL;
}
void consumer_send_method_setrequest()
{
    int count,threads;
    consumer_setup_test_param(&count,&threads);

    if(threads <= 0)
    {
        threads = 1;
    }
    
    printf("count=%d,threads=%d\n",count,threads);

    int loop = 0;
    pthread_t *tid = malloc(sizeof(pthread_t)*threads);
    for(;loop < threads;loop++)
    {
        pthread_create(&(tid[loop]),NULL,consumer_send_method_setrequest_thread,&count);
    }


    for(loop=0;loop < threads;loop++)
    {
        pthread_join((tid[loop]),NULL);    
    }
    sleep(1);
    printf("回收线程资源完成\n");
}


static uint32_t loop_request=0;
void * consumer_send_method_request_sync_thread(void *arg)
{
    int count = *(int *)arg; 
    int threadid = (int)pthread_self();
    int32_t Error;

    int loop=0;
    for(loop=0;loop<count;loop++)
    {
        //st_basic_com_form_resp在proto中的定义没有包含动态成员，所以是个静态固定长度结构体，所以可以使用xxx_size
        //作为buffer的静态大小定义
        uint8_t pbuffer_resp[st_basic_com_form_resp_size];
        uint32_t buffersize_resp=st_basic_com_form_resp_size;

        if(request_type == 0)
        {
            st_basic_com_form_req msgreq = st_basic_com_form_req_init_zero;

            uint8_t req_data_buffer[100];
            uint32_t tmp_loop_request = atomic_fetch_add(&loop_request,1);
            sprintf(req_data_buffer,"requeset-%d",tmp_loop_request);
            SOMB_INIT_BYTES_ENCODE_BUFFER(msgreq.req_data,req_data_buffer,sizeof(req_data_buffer));
            msgreq.req_int32 = tmp_loop_request;
            //st_basic_com_form_req在proto中的定义包含动态成员，所以是个动态长度结构体，所以不可以使用xxx_size
            //只能通过sizeof(xxx结构体)+动态变量的最大长度，得到一个整体的最大内存size, 在通过SOMB_ALLOC申请
            uint32_t buffersize_req=sizeof(st_basic_com_form_req)+sizeof(req_data_buffer);
            uint8_t *pbuffer_req = SOMB_ALLOC(buffersize_req);

            //reqmsg进行encode
            Error = SOMB_MESSAGE_ENCODE(st_basic_com_form_req,&msgreq,pbuffer_req,&buffersize_req);
            if( 0 != Error )
            {
                printf("consumer method req sync message encode request msg failed\n");
                SOMB_FREE(pbuffer_req);
                pthread_exit(&threadid);
                return NULL;
            }


            int32_t ret;
            ret = i32SOMB_Method_Request_Sync(BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1,pbuffer_req,buffersize_req, pbuffer_resp,&buffersize_resp,1500);
            if(SOMB_OK != ret)
            {
                printf("consumer method req sync(with req data) failed,loop=%d\n",loop_setrequest);
                SOMB_FREE(pbuffer_req);
                pthread_exit(&threadid);
            }
            SOMB_FREE(pbuffer_req);


            //respmsg进行decode
            st_basic_com_form_resp msgresp = st_basic_com_form_resp_init_zero;
            Error = SOMB_MESSAGE_DECODE(st_basic_com_form_resp,&msgresp,pbuffer_resp,buffersize_resp);
            if( 0 != Error )
            {
                printf("consumer method req sync message decode response msg failed\n");
                pthread_exit(&threadid);
                return NULL ;
            }

            printf("consumer recv sync-response:%s\n",msgresp.resp_data.bytes);
        }
        else
        {
            int32_t ret;
            ret = i32SOMB_Method_Request_Sync(BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2,NULL,0, pbuffer_resp,&buffersize_resp,1500);
            if(SOMB_OK != ret)
            {
                printf("consumer method req sync(without req data) failed,loop=%d\n",loop_setrequest);
                pthread_exit(&threadid);
            }
            else
            {
                //respmsg进行decode
                st_basic_com_form_resp msgresp = st_basic_com_form_resp_init_zero;
                Error = SOMB_MESSAGE_DECODE(st_basic_com_form_resp,&msgresp,pbuffer_resp,buffersize_resp);
                if( 0 != Error )
                {
                    printf("consumer method req sync message decode response msg failed\n");
                    pthread_exit(&threadid);
                    return NULL ;
                }
                printf("consumer recv sync-response:%s\n",msgresp.resp_data.bytes);

            }

        }
    }

    return NULL;
}
void consumer_send_method_request_sync()
{
    int count,threads;
    consumer_setup_test_param(&count,&threads);

    if(threads <= 0)
    {
        threads = 1;
    }

    printf("input the method request test:\n(0:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1 1:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2)\n");
    scanf("%d",&request_type);

    int loop = 0;
    pthread_t *tid = malloc(sizeof(pthread_t)*threads);
    for(;loop < threads;loop++)
    {
        pthread_create(&(tid[loop]),NULL,consumer_send_method_request_sync_thread,&count);
    }


    for(loop=0;loop < threads;loop++)
    {
        pthread_join((tid[loop]),NULL);    
    }
    sleep(1);
    printf("回收线程资源完成\n");
}

void consumer_method_request_async_cb(uint32_t ServiceId,void*MthRespData,uint32_t MthRespDataLen)
{
    //uint8_t pbuffer_resp[st_basic_com_form_resp_size];
    //uint32_t buffersize_resp=st_basic_com_form_resp_size;
    int32_t Error;

    st_basic_com_form_resp msgresp = st_basic_com_form_resp_init_zero;
    Error = SOMB_MESSAGE_DECODE(st_basic_com_form_resp,&msgresp,MthRespData,MthRespDataLen);
    if( 0 != Error )
    {
        printf("consumer method req sync message decode response msg failed\n");
        return ;
    }

    printf("consumer recv async-response:%s\n",msgresp.resp_data.bytes);
}
void* consumer_send_method_request_async_thread(void *arg)
{
    int count = *(int *)arg; 
    int threadid = (int)pthread_self();

    int loop=0;
    for(loop=0;loop<count;loop++)
    {
        if(request_type == 0)
        {
            st_basic_com_form_req msgreq = st_basic_com_form_req_init_zero;

            uint8_t req_data_buffer[100];
            uint32_t tmp_loop_request= atomic_fetch_add(&loop_request,1);
            msgreq.req_int32 =  tmp_loop_request;
            sprintf(req_data_buffer,"requeset-%d",tmp_loop_request);

            SOMB_INIT_BYTES_ENCODE_BUFFER(msgreq.req_data,req_data_buffer,sizeof(req_data_buffer));

            //st_basic_com_form_req在proto中的定义包含动态成员，所以是个动态长度结构体，所以不可以使用xxx_size
            //只能通过malloc申请空间作为buffer
            uint32_t buffersize_req=sizeof(st_basic_com_form_req)+sizeof(req_data_buffer);
            uint8_t *pbuffer_req = SOMB_ALLOC(buffersize_req);

            int32_t error;

            //编码
            error = SOMB_MESSAGE_ENCODE(st_basic_com_form_req,&msgreq,pbuffer_req,&buffersize_req);
            if( 0 != error )
            {
                printf("consumer method req async message encode failed,threadid=%d loop_request=%d\n",threadid,tmp_loop_request);
                SOMB_FREE(pbuffer_req);
                return NULL;
            }

            //发送
            int32_t ret;
            ret = i32SOMB_Method_Request_Async(BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1,pbuffer_req,buffersize_req,\
                    consumer_method_request_async_cb);
            if(SOMB_OK != ret)
            {
                printf("consumer method req async(with req data) failed,loop=%d\n",loop_setrequest);
            }

            //释放Buffer
            SOMB_FREE(pbuffer_req);
            
        }
        else
        {
            int32_t ret;
            ret = i32SOMB_Method_Request_Async(BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2,NULL,0,\
                    consumer_method_request_async_cb);
            if(SOMB_OK != ret)
            {
                printf("consumer method req async(without req data) failed,loop=%d\n",loop_setrequest);
            }

        }
    }
    return NULL;
}
void consumer_send_method_request_async()
{
    int count,threads;
    consumer_setup_test_param(&count,&threads);

    if(threads <= 0)
    {
        threads = 1;
    }
    printf("input the method request test:\n(0:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1 1:BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2)\n");
    scanf("%d",&request_type);
    int loop = 0;
    pthread_t *tid = malloc(sizeof(pthread_t)*threads);
    for(;loop < threads;loop++)
    {
        pthread_create(&(tid[loop]),NULL,consumer_send_method_request_async_thread,&count);
    }


    for(loop=0;loop < threads;loop++)
    {
        pthread_join((tid[loop]),NULL);    
    }
    sleep(1);
    printf("回收线程资源完成\n");
}

void consumer_listen_method_onvailable_cb(uint32_t ServiceId,bool bAvailable)
{
    printf("consumer listen method callback, recv serviceid=%08x is onvailbe=%d\n",ServiceId,bAvailable);

}
void consumer_listen_event_cb(uint32_t ServiceId,void*EvtData,uint32_t EvtDataLen)
{
    printf("consumer listen event callback, recv serviceid=%08x EvtDataLen=%d\n",ServiceId,EvtDataLen);

    int32_t Error;
    switch(ServiceId)
    {
        case BASICCOM_SID_EVENT_TEST:
            {
                st_basic_com_form_event EvtMsg = st_basic_com_form_event_init_zero;
                Error = SOMB_MESSAGE_DECODE(st_basic_com_form_event,&EvtMsg,EvtData,EvtDataLen);
                if( 0 != Error )
                {
                    printf("consumer event message decode failed\n");
                    return ;
                }
                printf("consumer event listen callback, recv data=%s\n",EvtMsg.data);
            }
            break;
        case BASICCOM_SID_EVENT_DATA_TEST:
            break;
        default:
            printf("###consumer event listen callback, recv unknown ServiceID=%d\n",ServiceId);
            break;
    }


}
void consumer_add_event_listen_test()
{
    int32_t ret;

    ST_SOMB_SERVICE_LISTEN_INFO stEventListenInfoList[]=
    {
        {
            .ServiceId=BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1,
            .ServiceType = SOMB_SVC_TYPE_METHOD,
            .OnAvailableCB = consumer_listen_method_onvailable_cb,
        },
        {
            .ServiceId=BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2,
            .ServiceType = SOMB_SVC_TYPE_METHOD,
            .OnAvailableCB = consumer_listen_method_onvailable_cb,
        },
        {
            .ServiceId = BASICCOM_SID_EVENT_TEST,
            .ServiceType = SOMB_SVC_TYPE_EVENT,
            .ListenerFlags = SOMB_SVC_FLAGS_EVENT_LISTEN_SYNC_DATA, //this flags means the event data will sync to you when it is deployed by it's provider
            .ListenerCB = consumer_listen_event_cb,
        },
    };

    ret = i32SOMB_ServiceListener_Add(stEventListenInfoList,ARRAY_SIZE(stEventListenInfoList));
    if(0 != ret)
    {
        printf("consumer event listener add failed\n");
    }

}

void consumer_del_event_listen_test()
{
    int32_t ret;
    uint32_t EventServiceIdList[]=
    {
        BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST1,
        BASICCOM_SID_METHOD_REQUEST_RESPONSE_TEST2,
    };

    ret = i32SOMB_EventListener_Delete(EventServiceIdList,ARRAY_SIZE(EventServiceIdList));
    if(0 != ret)
    {
        printf("consumer event listener delete failed\n");
    }
    return ;
}



void consumer_event_data_test_cb(uint32_t ServiceId,void*EvtData,uint32_t EvtDataLen)
{
    printf("event data test callback:\nrecv serviceid=%08x EvtDataLen=%d\n",ServiceId,EvtDataLen);

    int32_t Error;
    switch(ServiceId)
    {
        case BASICCOM_SID_EVENT_DATA_TEST:
            {
                st_event_data_test TestMsg = st_event_data_test_init_zero;
                Error = SOMB_MESSAGE_DECODE(st_event_data_test,&TestMsg,EvtData,EvtDataLen);
                if( 0 != Error )
                {
                    printf("event data test message decode failed\n");
                    return ;
                }
                printf("event data test callback, recv data=%d\n",TestMsg.i32data);
            }
            break;
        default:
            printf("###event data test callback, recv unknown ServiceID=%d\n",ServiceId);
            break;
    }


}
void consumer_event_data_test()
{
    int32_t ret;

    ST_SOMB_SERVICE_LISTEN_INFO stEventListenInfoList[]=
    {
        {
            .ServiceId=BASICCOM_SID_EVENT_DATA_TEST,
            .ServiceType = SOMB_SVC_TYPE_EVENT,
            .ListenerCB= consumer_event_data_test_cb,
        },
    };

    ret = i32SOMB_ServiceListener_Add(stEventListenInfoList,ARRAY_SIZE(stEventListenInfoList));
    if(0 != ret)
    {
        printf("event data test listener add failed\n");
    }
}

void consumer_method_rrs_data_test(void)
{
    st_rrs_data_test data = st_rrs_data_test_init_zero;
    uint8_t buffer[st_rrs_data_test_size]={0};
    uint32_t buffersize  = st_rrs_data_test_size;
    int32_t error;
    error = SOMB_MESSAGE_ENCODE(st_rrs_data_test ,&data,buffer,&buffersize);
    if( 0 != error )
    {
        printf("consumer method rrs encode failed\n");
        return;
    }


    uint8_t resp_buffer[st_rrs_data_test_size]={0};
    uint32_t resp_buffersize  = st_rrs_data_test_size;
    error = i32SOMB_Method_Request_Sync(BASICCOM_SID_METHOD_RRS_DATA_TEST,buffer,buffersize,resp_buffer,&resp_buffersize,1000);
    if(SOMB_OK != error)
    {
        printf("consumer method rrs request sync failed\n");
        return;
    }

    st_rrs_data_test resp_data = st_rrs_data_test_init_zero;
    error = SOMB_MESSAGE_DECODE(st_rrs_data_test,&resp_data,resp_buffer,resp_buffersize);
    if( 0 != error )
    {
        printf("consumer methold rrs data test decode response msg failed\n");
        return ;
    }

    if(data.i32data == resp_data.i32data)
    {
        printf("consumer mthold rrs data test success\n");
    }
}

void consumer_method_rra_data_test_cb(uint32_t ServiceId,void*MthRespData,uint32_t MthRespDataLen)
{
    st_rra_data_test resp_data = st_rra_data_test_init_zero;
    int32_t error = SOMB_MESSAGE_DECODE(st_rra_data_test,&resp_data,MthRespData,MthRespDataLen);
    if( 0 != error )
    {
        printf("consumer methold rra data test decode response msg failed\n");
        return ;
    }

    printf("consumer mthold rra data test recv:%d\n",resp_data.i32data);

}
void consumer_method_rra_data_test(void)
{
    st_rra_data_test data = st_rra_data_test_init_zero;
    uint8_t buffer[st_rra_data_test_size]={0};
    uint32_t buffersize  = st_rra_data_test_size;
    int32_t error;
    error = SOMB_MESSAGE_ENCODE(st_rra_data_test ,&data,buffer,&buffersize);
    if( 0 != error )
    {
        printf("consumer method rra encode failed\n");
        return;
    }


    error = i32SOMB_Method_Request_Async(BASICCOM_SID_METHOD_RRA_DATA_TEST,buffer,buffersize,consumer_method_rra_data_test_cb);
    if(SOMB_OK != error)
    {
        printf("consumer method rra request sync failed\n");
        return;
    }
    printf("consumer mthold rra data test send:%d\n",data.i32data);

}

void consumer_method_setr_data_test(void)
{
    st_setr_data_test data = st_setr_data_test_init_zero;
    uint8_t buffer[st_setr_data_test_size]={0};
    uint32_t buffersize  = st_setr_data_test_size;
    int32_t error;
    error = SOMB_MESSAGE_ENCODE(st_setr_data_test ,&data,buffer,&buffersize);
    if( 0 != error )
    {
        printf("consumer method setr encode failed\n");
        return;
    }


    error = i32SOMB_Method_SetRequest(BASICCOM_SID_METHOD_SETR_DATA_TEST,buffer,buffersize);
    if(SOMB_OK != error)
    {
        printf("consumer method setr request sync failed\n");
        return;
    }
    printf("consumer mthold setr data test send:%d\n",data.i32data);

}
void print_usage()
{
    printf("---------------------------\n");
    printf("1: method set request test\n");
    printf("2: method request-sync test\n");
    printf("3: method request-async test\n");
    printf("4: add service listen test\n");
    printf("5: del service listen test\n");
    printf("---------------------------\n");

}
int main(void)
{
    uint32_t SubNsId;
    int32_t ret = i32SOMB_SubNs_Init(&SubNsId);
    if(SOMB_OK != ret)
    {
        printf("consumer somb sub ns init failed\n");
        return -1;
    }
    printf("SubNs=%d SOMB Init success\n",SubNsId);

    //uint32_t loop = 0;
    bool keep=false;
    while(1)
    {
        if(!keep)
            print_usage();

        printf("chose your test index:\n");
        int cmd;
        scanf("%d",&cmd);
        printf("your chose is %d \n",cmd);
        keep = false;
        switch(cmd)
        {
            case 1:
                consumer_send_method_setrequest();
                break;
            case 2:
                consumer_send_method_request_sync();
                break;
            case 3:
                consumer_send_method_request_async();
                break;
            case 4:
                consumer_add_event_listen_test();
                break;
            case 5:
                consumer_del_event_listen_test();
                break;
            case 6:
                consumer_event_data_test();
                break;
            case 7:
                consumer_method_rrs_data_test();
                break;
            case 8:
                consumer_method_rra_data_test();
                break;
            case 9:
                consumer_method_setr_data_test();
                break;
            default:
                keep = true;
                break;
        }

    }

    return 0;
}
